package com.project.communitymanagement.service.impl;

import com.project.communitymanagement.common.BaseServiceImpl;
import com.project.communitymanagement.common.Page;
import com.project.communitymanagement.entity.TbOwner;
import com.project.communitymanagement.mapper.TbOwnerMapper;
import com.project.communitymanagement.service.TbOwnerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ProjectName: community-management
 * @Package: com.project.communitymanagement.service.impl
 * @ClassName: TbOwnerServiceImpl
 * @Author: "XGP"
 * @Description:
 * @Date: 2022/3/13 12:31
 * @Version: 1.0
 */
@Service
public class TbOwnerServiceImpl  extends BaseServiceImpl<TbOwnerMapper, TbOwner> implements TbOwnerService {

    @Autowired
    private TbOwnerMapper tbownerMapper;

    @Override
    protected TbOwnerMapper mapper() {
        return tbownerMapper;
    }

    @Override
    protected TbOwner defaultModel() {
        return new TbOwner();
    }

    @Override
    public Page<TbOwner> getPages(Page<TbOwner> page) {
        if(!("admin".equals(page.getRequestData().getCreate_user()))){
            page.getRequestData().setUsername(page.getRequestData().getCreate_user());
        }
        return super.getPages(page);
    }

    @Override
    public int save(TbOwner tbOwner) {
        if(tbOwner.getId() == null){
            TbOwner username = tbownerMapper.getUserByUsername(tbOwner.getUsername());
            if(username != null){
                throw new RuntimeException("当前用户已存在，请勿重复添加！");
            }
        }
        return super.save(tbOwner);
    }

    @Override
    public List<TbOwner> getAll() {
        return tbownerMapper.getAll();
    }
}

